#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright © 2005-2007 Kaufmann Manuel
#
# This file is part of Tweety-Finger.
#
# Tweety-Finger is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Tweety-Finger is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tweety-Finger; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
# USA

import sys
import pygame
from pygame.locals import *

class Menu(object):
	def __init__(self, window, font, fontSelected, color, colorSelected, posx, posy, spacey, options, soundUpMove, soundDownMove, soundEnter, backImage, listMenus, listFunctions):
		
		"""
			Inicializador de la clase, recibe la ventana donde la
			va a mostrar, las fuentes, colores, posiciones,
			opciones y el espacio entre cada opción
		"""
		
		# Sonidos
		self.soundDownMove = soundDownMove
		self.soundUpMove = soundUpMove
		self.soundEnter = soundEnter
		
		# Fuentes
		self.font = font
		self.fontSelected = fontSelected
		
		# Colores
		self.color = color
		self.colorSelected = colorSelected
		
		# Posiciones
		self.posx = posx
		self.posy = posy
		self.spacey = spacey
		
		self.window = window
		self.backImage = backImage
		self.optionSelected = 0
		self.done = False
		
		# Matriz de opciones, menúes y funciones
		self.matrixMenu = []
		self.matrixMenu.append(options)
		self.matrixMenu.append(listMenus)
		self.matrixMenu.append(listFunctions)

	def refreshMenu(self):
		"""
			Refresca el menu mostrando si hay una
			opción seleccionada
		"""
		
		self.window.setBackground("menu", self.backImage)
		
		for option in self.matrixMenu[0]:
			if self.isSelected(option):
				text = self.fontSelected.render(option, 1,\
							 self.colorSelected)
			else:
				text = self.font.render(option, 1, self.color)
			textpos = text.get_rect()
			textpos.centerx = self.posx
			textpos.centery = self.posy + \
					self.matrixMenu[0].index(option)*self.spacey
			self.window.screen.blit(text, textpos)
			pygame.display.flip()
		
	def isSelected(self, option):
		"""
			Determina si la opcion pasada esta seleccionada
			actualmente
		"""
		
		if self.matrixMenu[0].index(option) == self.optionSelected:
			return True
		else:
			return False
	
	def previous(self):
		"""
			Retrocede 1 en la lista de opciones
		"""
		
		if self.optionSelected == 0:
			self.optionSelected = len(self.matrixMenu[0])-1
		else:
			self.optionSelected -= 1
		self.refreshMenu()
		self.soundUpMove.play()
	
	def next(self):
		"""
			Avanza 1 en la lista de opciones
		"""
		
		if self.optionSelected == len(self.matrixMenu[0])-1:
			self.optionSelected = 0
		else:
			self.optionSelected += 1
		self.refreshMenu()
		self.soundDownMove.play()

	def enterMenu(self, menu, function):
		"""
			Ingresa a la opción seleccionada o ejecuta la function
			pasa por argumento si esta es distinta de None
		"""
		
		
		self.soundEnter.play()
		if function != None:
			function(self.matrixMenu[0][self.optionSelected])
		elif menu != None:
			menu.refreshMenu()
			menu.waitOption()
	
	def waitOption(self):
		"""
			Espera a que el usuario ingrese una opción
		"""
		
		self.refreshMenu()
		eventType = None
		while eventType != QUIT:
			event = pygame.event.wait()
			eventType = event.type
			if eventType == KEYDOWN:
				if event.key in [K_RETURN, K_KP_ENTER]:
					self.enterMenu(self.matrixMenu[1][self.optionSelected], self.matrixMenu[2][self.optionSelected])
				elif event.key == K_UP:
					self.previous()
				elif event.key == K_DOWN:
					self.next()
				elif event.key == K_ESCAPE:
					break
			if eventType == KEYUP and event.key == K_ESCAPE:
				self.refreshMenu()
		if eventType == QUIT:
			sys.exit("Quit")
